﻿using System.Globalization;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;


namespace FairElectionsAnalyzer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List<Candidate> candidates = new List<Candidate>
                {
                    new Candidate("Дегтярев"),
                    new Candidate("Левичев"),
                    new Candidate("Мельников"),
                    new Candidate("Митрохин"),
                    new Candidate("Навальный"),
                    new Candidate("Собянин")
                };


        public List<string> districtUrls = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            #region Райноы
            //Ссылки на результаты по всем 133-м районам:
            districtUrls = new List<string>
                {
                    /*1 р*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177400103&tvd=27720001368301&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368301&type=222",
                    /*2 р*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177400203&tvd=27720001368299&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368299&type=222",
                    /*3 р*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177400303&tvd=27720001368300&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368300&type=222",
                    /*4 р*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177400403&tvd=27720001368297&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368297&type=222",
                    /*5 р*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177400503&tvd=27720001368298&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368298&type=222",
                    /*6 р*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177400603&tvd=27720001368311&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368311&type=222",
                    /*7 р*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177400703&tvd=27720001368310&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368310&type=222",
                    /*8 р*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177400803&tvd=27720001368307&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368307&type=222",
                    /*9 р*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177400903&tvd=27720001368309&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368309&type=222",
                    /*10 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177401003&tvd=27720001368308&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368308&type=222",
                    /*11 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177401103&tvd=27720001368306&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368306&type=222",
                    /*12 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177401203&tvd=27720001368304&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368304&type=222",
                    /*13 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177401303&tvd=27720001368305&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368305&type=222",
                    /*14 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177401403&tvd=27720001368302&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368302&type=222",
                    /*15 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177401503&tvd=27720001368303&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368303&type=222",
                    /*16 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177401603&tvd=27720001368320&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368320&type=222",
                    /*17 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177401703&tvd=27720001368319&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368319&type=222",
                    /*18 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177401803&tvd=27720001368318&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368318&type=222",
                    /*19 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177401903&tvd=27720001368317&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368317&type=222",
                    /*20 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177402003&tvd=27720001368401&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368401&type=222",
                    /*22 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177402203&tvd=27720001368400&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368400&type=222",
                    /*24 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177402403&tvd=27720001368313&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368313&type=222",
                    /*25 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177402503&tvd=27720001368312&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368312&type=222",
                    /*26 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177402603&tvd=27720001368326&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368326&type=222",
                    /*27 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177402703&tvd=27720001368323&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368323&type=222",
                    /*28 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177402803&tvd=27720001368324&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368324&type=222",
                    /*29 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177402903&tvd=27720001368325&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368325&type=222",
                    /*30 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177403003&tvd=27720001368321&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368321&type=222",
                    /*31 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177403103&tvd=27720001368322&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368322&type=222",
                    /*32 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177403203&tvd=27720001368356&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368356&type=222",
                    /*33 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177403303&tvd=27720001368355&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368355&type=222",
                    /*34 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177403403&tvd=27720001368353&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368353&type=222",
                    /*35 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177403503&tvd=27720001368354&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368354&type=222",
                    /*37 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177403703&tvd=27720001368329&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368329&type=222",
                    /*38 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177403803&tvd=27720001368402&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368402&type=222",
                    /*39 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177403903&tvd=27720001368327&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368327&type=222",
                    /*40 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177404003&tvd=27720001368330&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368330&type=222",
                    /*41 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177404103&tvd=27720001368328&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368328&type=222",
                    /*42 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177404203&tvd=27720001368403&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368403&type=222",
                    /*43 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177404303&tvd=27720001368404&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368404&type=222",
                    /*44 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177404403&tvd=27720001368334&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368334&type=222",
                    /*45 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177404503&tvd=27720001368333&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368333&type=222",
                    /*46 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177404603&tvd=27720001368335&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368335&type=222",
                    /*47 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177404703&tvd=27720001368405&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368405&type=222",
                    /*48 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177404803&tvd=27720001368406&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368406&type=222",
                    /*49 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177404903&tvd=27720001368332&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368332&type=222",
                    /*50 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177405003&tvd=27720001368331&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368331&type=222",
                    /*51 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177405103&tvd=27720001368342&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368342&type=222",
                    /*52 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177405203&tvd=27720001368341&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368341&type=222",
                    /*53 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177405303&tvd=27720001368407&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368407&type=222",
                    /*54 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177405403&tvd=27720001368408&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368408&type=222",
                    /*55 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177405503&tvd=27720001368340&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368340&type=222",
                    /*56 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177405603&tvd=27720001368337&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368337&type=222",
                    /*57 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177405703&tvd=27720001368339&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368339&type=222",
                    /*58 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177405803&tvd=27720001368338&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368338&type=222",
                    /*59 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177405903&tvd=27720001368336&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368336&type=222",
                    /*60 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177406003&tvd=27720001368348&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368348&type=222",
                    /*61 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177406103&tvd=27720001368346&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368346&type=222",
                    /*62 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177406203&tvd=27720001368345&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368345&type=222",
                    /*63 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177406303&tvd=27720001368344&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368344&type=222",
                    /*64 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177406403&tvd=27720001368295&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368295&type=222",
                    /*65 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177406503&tvd=27720001368421&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368421&type=222",
                    /*66 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177406603&tvd=27720001368420&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368420&type=222",
                    /*67 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177406703&tvd=27720001368343&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368343&type=222",
                    /*68 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177406803&tvd=27720001368419&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368419&type=222",
                    /*69 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177406903&tvd=27720001368360&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368360&type=222",
                    /*70 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177407003&tvd=27720001368361&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368361&type=222",
                    /*71 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177407103&tvd=27720001368357&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368357&type=222",
                    /*72 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177407203&tvd=27720001368358&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368358&type=222",
                    /*73 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177407303&tvd=27720001368359&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368359&type=222",
                    /*74 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177407403&tvd=27720001368418&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368418&type=222",
                    /*75 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177407503&tvd=27720001368350&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368350&type=222",
                    /*76 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177407603&tvd=27720001368349&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368349&type=222",
                    /*77 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177407703&tvd=27720001368347&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368347&type=222",
                    /*78 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177407803&tvd=27720001368367&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368367&type=222",
                    /*79 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177407903&tvd=27720001368366&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368366&type=222",
                    /*80 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177408003&tvd=27720001368364&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368364&type=222",
                    /*81 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177408103&tvd=27720001368365&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368365&type=222",
                    /*82 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177408203&tvd=27720001368416&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368416&type=222",
                    /*83 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177408303&tvd=27720001368411&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368411&type=222",
                    /*84 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177408403&tvd=27720001368363&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368363&type=222",
                    /*85 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177408503&tvd=27720001368414&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368414&type=222",
                    /*86 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177408603&tvd=27720001368412&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368412&type=222",
                    /*87 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177408703&tvd=27720001368362&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368362&type=222",
                    /*88 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177408803&tvd=27720001368296&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368296&type=222",
                    /*89 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177408903&tvd=27720001368372&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368372&type=222",
                    /*90 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177409003&tvd=27720001368371&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368371&type=222",
                    /*91 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177409103&tvd=27720001368413&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368413&type=222",
                    /*92 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177409203&tvd=27720001368369&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368369&type=222",
                    /*93 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177409303&tvd=27720001368415&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368415&type=222",
                    /*94 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177409403&tvd=27720001368417&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368417&type=222",
                    /*95 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177409503&tvd=27720001368370&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368370&type=222",
                    /*96 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177409603&tvd=27720001368368&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368368&type=222",
                    /*97 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177409703&tvd=27720001368398&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368398&type=222",
                    /*98 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177409803&tvd=27720001368399&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368399&type=222",
                    /*99 */  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177409903&tvd=27720001368395&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368395&type=222",
                    /*100*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177410003&tvd=27720001368396&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368396&type=222",
                    /*101*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177410103&tvd=27720001368397&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368397&type=222",
                    /*102*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177410203&tvd=27720001368393&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368393&type=222",
                    /*103*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177410303&tvd=27720001368394&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368394&type=222",
                    /*104*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177410403&tvd=27720001368392&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368392&type=222",
                    /*105*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177410503&tvd=27720001368391&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368391&type=222",
                    /*106*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177410603&tvd=27720001368390&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368390&type=222",
                    /*107*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177410703&tvd=27720001368389&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368389&type=222",
                    /*108*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177410803&tvd=27720001368386&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368386&type=222",
                    /*109*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177410903&tvd=27720001368387&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368387&type=222",
                    /*110*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177411003&tvd=27720001368388&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368388&type=222",
                    /*111*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177411103&tvd=27720001368384&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368384&type=222",
                    /*112*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177411203&tvd=27720001368383&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368383&type=222",
                    /*113*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177411303&tvd=27720001368385&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368385&type=222",
                    /*114*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177411403&tvd=27720001368380&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368380&type=222",
                    /*115*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177411503&tvd=27720001368381&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368381&type=222",
                    /*116*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177411603&tvd=27720001368382&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368382&type=222",
                    /*117*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177411703&tvd=27720001368378&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368378&type=222",
                    /*119*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177411903&tvd=27720001368377&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368377&type=222",
                    /*120*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177412003&tvd=27720001368379&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368379&type=222",
                    /*121*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177412103&tvd=27720001368374&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368374&type=222",
                    /*122*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177412203&tvd=27720001368376&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368376&type=222",
                    /*123*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177412303&tvd=27720001368375&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368375&type=222",
                    /*124*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177412403&tvd=27720001368373&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368373&type=222",
                    /*125*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177412503&tvd=27720001368410&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368410&type=222",
                    /*128*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177412803&tvd=27720001368409&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368409&type=222",
                    /*129*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177412903&tvd=27720001368315&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368315&type=222",
                    /*130*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177413003&tvd=27720001368316&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368316&type=222",
                    /*131*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177413103&tvd=27720001368314&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368314&type=222",
                    /*132*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177413203&tvd=27720001368351&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368351&type=222",
                    /*133*/  "http://www.moscow_city.vybory.izbirkom.ru/region/region/moscow_city?action=show&root=177413303&tvd=27720001368352&vrn=27720001368289&region=77&global=&sub_region=77&prver=0&pronetvd=null&vibid=27720001368352&type=222"
                };
            #endregion
            backgroundWorker1.RunWorkerAsync();

            Thread.CurrentThread.CurrentCulture = new CultureInfo(1033);
            ChrtGraph3.ChartAreas[0].AxisX.Interval = 0.5;
            ChrtGraph3.ResetAutoValues();
            ChrtGraph3.Series[0].LegendText = "Количество УИК";
            ChrtGraph3.Series[0].Points.Clear();
            //ChrtGraph3.ChartAreas[0].AxisX.Maximum = 100; //100%
            tabControl1.SelectedTab = tabControl1.TabPages[1];

            foreach (Candidate candidate in candidates)
            {
                CbCandidate.Items.Add(candidate.CanditateName);

            }
            CbCandidate.SelectedIndex = 5;

            lbUikNum.Text = candidates[0].Points.Count.ToString();

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void CbCandidate_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            lbUikNum.Text = candidates[0].Points.Count.ToString();
            TBdebut.Clear();
            ChrtGraph3.Refresh();
            ChrtGraph3.Series[0].Points.Clear();
            

            var groupedResults =
                candidates[CbCandidate.SelectedIndex].Points.AsEnumerable()
                                                     .GroupBy(x => x.Result)
                                                     .Select(group => new {result = group.Key, Count = group.Count()})
                                                     .OrderBy(x => x.result);


            foreach (var t in groupedResults)
            {
                TBdebut.Text += string.Format("{0},{1}", t.Count, t.result) + Environment.NewLine;

                ChrtGraph3.Series[0].Points.AddXY(t.result, t.Count );
            }
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(1033);
            for (int index = 0; index < districtUrls.Count; index++)
            {
                var districtUrl = districtUrls[index];
                Distrinct district = new Distrinct(districtUrl);

                for (int i = 0; i < candidates.Count; i++)
                {
                    candidates[i].Points.AddRange(district.Candidates[i].Points);
                }
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            CbCandidate.SelectedIndex = 5;
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            CbCandidate_SelectedIndexChanged_1(null,null);
        }

        private void оПрограммеToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 ab = new AboutBox1();
            ab.ShowDialog();
        }
    }

    public class Candidate
    {
        public String CanditateName { get; set; }
        public List<UikResult> Points { get; set; }

        public Candidate(string Name)
        {
            Points = new List<UikResult>();
            CanditateName = Name;
        }
    }

    public class UikResult
    {
        public String UikName { get; set; }
        public double Result { get; set; }
    }

    public class Distrinct
    {
        public int NumberOfUiks { get; set; }
        public string DistrictName { get; set; }
        public string DistrictUrl { get; set; }
        public List<Candidate> Candidates { get; set; }

        public Distrinct(string districtUrl)
        {
            try
            {
                Candidates = new List<Candidate>();
                DistrictUrl = districtUrl;

                HtmlWeb web = new HtmlWeb {OverrideEncoding = Encoding.Default};

                HtmlAgilityPack.HtmlDocument doc = web.Load(districtUrl);

                var lastOrDefault = doc.DocumentNode.Descendants().LastOrDefault(x => x.InnerText.StartsWith("район"));
                if (lastOrDefault != null)
                    DistrictName = lastOrDefault.InnerText;

                HtmlNode voteTable = doc.DocumentNode.Descendants().LastOrDefault(x => x.Name == "table");

                NumberOfUiks =
                    voteTable.Descendants()
                             .First(x => x.Name == "tr")
                             .Descendants()
                             .Where(x => x.Name == "td")
                             .Skip(13)
                             .Count();

                List<string> UIKs = 
                    voteTable.Descendants()
                             .First(x => x.Name == "tr")
                             .Descendants()
                             .Where(x => x.Name == "td")
                             .Select(x => x.InnerText.Trim())
                             .ToList();

                var degtyarevs2 =
                    voteTable.ChildNodes.Where(x => x.Name == "tr")
                             .Skip(19)
                             .First()
                             .ChildNodes.Where(x => x.InnerText.Contains("%")).Select(x => x.LastChild.InnerText.Trim().Replace("%", ""));

                List<double> degtyarevs =
                    voteTable.ChildNodes.Where(x => x.Name == "tr")
                             .Skip(19)
                             .First()
                             .ChildNodes.Where(x => x.InnerText.Contains("%"))
                             .Select(
                                 x =>
                                 Math.Round(double.Parse(x.LastChild.InnerText.Trim().Replace("%", ""))*2,
                                            MidpointRounding.AwayFromZero)/2)
                             .ToList();
                List<double> levichevs =
                    voteTable.ChildNodes.Where(x => x.Name == "tr")
                             .Skip(20)
                             .First()
                             .ChildNodes.Where(x => x.InnerText.Contains("%"))
                             .Select(
                                 x =>
                                 Math.Round(double.Parse(x.LastChild.InnerText.Trim().Replace("%", ""))*2,
                                            MidpointRounding.AwayFromZero)/2)
                             .ToList();
                List<double> melnikovs =
                    voteTable.ChildNodes.Where(x => x.Name == "tr")
                             .Skip(21)
                             .First()
                             .ChildNodes.Where(x => x.InnerText.Contains("%"))
                             .Select(
                                 x =>
                                 Math.Round(double.Parse(x.LastChild.InnerText.Trim().Replace("%", ""))*2,
                                            MidpointRounding.AwayFromZero)/2)
                             .ToList();
                List<double> mitrokhins =
                    voteTable.ChildNodes.Where(x => x.Name == "tr")
                             .Skip(22)
                             .First()
                             .ChildNodes.Where(x => x.InnerText.Contains("%"))
                             .Select(
                                 x =>
                                 Math.Round(double.Parse(x.LastChild.InnerText.Trim().Replace("%", ""))*2,
                                            MidpointRounding.AwayFromZero)/2)
                             .ToList();
                List<double> navalnys =
                    voteTable.ChildNodes.Where(x => x.Name == "tr")
                             .Skip(23)
                             .First()
                             .ChildNodes.Where(x => x.InnerText.Contains("%"))
                             .Select(
                                 x =>
                                 Math.Round(double.Parse(x.LastChild.InnerText.Trim().Replace("%", "")) * 2,
                                            MidpointRounding.AwayFromZero) / 2)
                             .ToList();

                List<double> sobyanins =
                    voteTable.ChildNodes.Where(x => x.Name == "tr")
                             .Skip(24)
                             .First()
                             .ChildNodes.Where(x => x.InnerText.Contains("%"))
                             .Select(
                                 x =>
                                 Math.Round(double.Parse(x.LastChild.InnerText.Trim().Replace("%", "")) * 2,
                                            MidpointRounding.AwayFromZero) / 2)
                             .ToList();

                Candidates.Add(new Candidate("Дегтярев"));
                Candidates.Add(new Candidate("Левичев"));
                Candidates.Add(new Candidate("Мельников"));
                Candidates.Add(new Candidate("Митрохин"));
                Candidates.Add(new Candidate("Навальный"));
                Candidates.Add(new Candidate("Собянин"));

                for (int i = 0; i < UIKs.Count; i++)
                {
                    Candidates[0].Points.Add(new UikResult() {UikName = UIKs[i], Result = degtyarevs[i]});
                    Candidates[1].Points.Add(new UikResult() {UikName = UIKs[i], Result = levichevs[i]});
                    Candidates[2].Points.Add(new UikResult() {UikName = UIKs[i], Result = melnikovs[i]});
                    Candidates[3].Points.Add(new UikResult() {UikName = UIKs[i], Result = mitrokhins[i]});
                    Candidates[4].Points.Add(new UikResult() {UikName = UIKs[i], Result = navalnys[i]});
                    Candidates[5].Points.Add(new UikResult() {UikName = UIKs[i], Result = sobyanins[i]});
                }
            }
            catch (Exception Exception)
            {
                string d = DistrictUrl;
            }
        }
    }
}
